import { BehaviorSubject, Subject } from "rxjs";

// BehaviorSubject 示例
const behaviorSubject = new BehaviorSubject<number>(0); // 初始值0

behaviorSubject.subscribe((value) =>
  console.log(`BehaviorSubject 订阅者A: ${value}`),
);
// 立即输出：BehaviorSubject 订阅者A: 0

behaviorSubject.next(1);
// 输出：BehaviorSubject 订阅者A: 1

behaviorSubject.subscribe((value) =>
  console.log(`BehaviorSubject 订阅者B: ${value}`),
);
// 立即输出：BehaviorSubject 订阅者B: 1

// Subject 示例
const subject = new Subject<number>();

subject.subscribe((value) => console.log(`Subject 订阅者A: ${value}`));
// 不会立即输出，因为还没有值

subject.next(1);
// 输出：Subject 订阅者A: 1

subject.next(2);
// 输出：Subject 订阅者A: 2

subject.subscribe((value) => console.log(`Subject 订阅者B: ${value}`));
// 不会立即输出，因为Subject不会保存历史值

subject.next(3);
// 输出：Subject 订阅者A: 3
// 输出：Subject 订阅者B: 3
